import {METHOD, CLIENT} from './config.js'
import {sendRequest, mobileUrl} from './base.js'

// 查询【默认地址】
const ADDRESS_QUERY_DEFAULT_ADDRESS = 'address/queryDefaultAddress/'

// 查询全部【地区列表2】
const ADDRESS_QUERY_ALL_REGION2 = 'region/queryAllRegion2'

// 添加地址 POST
const ADDRESS_SAVE_NEW_DATA = 'address/saveAddress'

// 编辑地址 POST
const ADDRESS_UPDATE_DATA = 'address/updateAddress'

// 查询【我的地址列表】2 GET
const ADDRESS_QUERY_ADDRESSES = 'address/queryAddresses/'

// 设置为默认地址 GET
const ADDRESS_SET_DEFAULT = 'address/updateAddressDefault/'

// 删除地址 GET
const ADDRESS_DELETE_ADDRESS = 'address/deleteAddress/'

/**
 * 分页获取用户地址列表
 * @param passportId
 * {passportId:'通行证ID'}
 * @returns {*}
 */
export function queryDefaultAddress(passportId) {
  return sendRequest(mobileUrl(ADDRESS_QUERY_DEFAULT_ADDRESS + passportId))
}

/**
 * 查询全部【地区列表】
 * @param params
 * @returns {*}
 */
export function queryAllRegion2(params) {
  return sendRequest(mobileUrl(ADDRESS_QUERY_ALL_REGION2), params)
}

/**
 * 添加地址
 * @param params
 * {
 *  passportId: '通行证ID',
 *  name: '收货人姓名',
 *  mobileCode: '收货人手机号',
 *  provinceId: '省份ID',
 *  cityId: '地市ID',
 *  districtId: '区县ID',
 *  detail: '详细地址',
 *  isDefault: '是否是默认地址：Y-是；N-否'
 * }
 * @returns {*}
 */
export function saveAddress(params) {
  return sendRequest(mobileUrl(ADDRESS_SAVE_NEW_DATA), params, METHOD.POST)
}

/**
 * 编辑地址
 * @param params
 * {
 *  id: 'id',
 *  passportId: '通行证ID',
 *  name: '收货人姓名',
 *  mobileCode: '收货人手机号',
 *  provinceId: '省份ID',
 *  cityId: '地市ID',
 *  districtId: '区县ID',
 *  detail: '详细地址',
 *  isDefault: '是否是默认地址：Y-是；N-否'
 * }
 * @returns {*}
 */
export function updateAddress(params) {
  return sendRequest(mobileUrl(ADDRESS_UPDATE_DATA), params, METHOD.POST)
}

/**
 * deleteAddress
 * @param params
 * @returns {*}
 */
export function setDefault(id) {
  return sendRequest(mobileUrl(ADDRESS_SET_DEFAULT + id))
}

/**
 * 删除地址
 * @param id
 * @returns {*}
 */
export function deleteAddress(id) {
  return sendRequest(mobileUrl(ADDRESS_DELETE_ADDRESS + id))
}

/**
 * 查询【我的地址列表】2
 * @param passportId
 * @param params
 * @returns {*}
 */
export function queryAddresses(passportId, params) {
  let data = Object.assign(params, {clientCode: CLIENT})
  return sendRequest(mobileUrl(ADDRESS_QUERY_ADDRESSES + passportId), data)
}
